﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Configuration;
using System.IO;
using eBay.Service.Core.Soap;

namespace eBayAPICall
{
    public class ApiContextSectionHandler
    {
        private Dictionary<string,ApiContextConfigEntity> dicConfigs = new Dictionary<string,ApiContextConfigEntity>();

        private XmlDocument configDoc = new XmlDocument();
        private string DEFAULT_CONFIG_FILE = "ApiContext.config";

        private static ApiContextSectionHandler instance = null;
        private static object lockObj = new object();
        private ApiContextSectionHandler() 
        {
            dicConfigs = new Dictionary<string,ApiContextConfigEntity>();
            string configFileName = ConfigurationManager.AppSettings["ApiContextConfigFile"];
            if (string.IsNullOrEmpty(configFileName))
                configFileName = DEFAULT_CONFIG_FILE;

            if (!File.Exists(configFileName))
                throw new FileNotFoundException(string.Format("ApiContext config file not found, be make sure {0} exists", configFileName), configFileName);

            configDoc.Load(configFileName);
        }

        public static ApiContextSectionHandler GetInstance()
        {
            if (instance == null)
            {
                lock (lockObj)
                {
                    if (instance == null)
                    {
                        instance = new ApiContextSectionHandler();
                    }
                }
            }

            return instance;
        }

        public ApiContextConfigEntity GetConfig(string configName)
        {
            if (string.IsNullOrEmpty(configName))
                throw new ArgumentNullException("configName");

            if (!dicConfigs.ContainsKey(configName))
            {
                XmlNode node = configDoc.SelectSingleNode(string.Format("ApiContextConfigGroup/ApiContextConfig[@Id='{0}']", configName));

                if (node == null)
                    throw new Exception("Node name in config file not exists");

                ApiContextConfigEntity configEntity = new ApiContextConfigEntity();
                configEntity.SoapApiServerUrl = node.SelectSingleNode("SoapApiServerUrl").InnerText.Trim();
                configEntity.eBayToken = node.SelectSingleNode("eBayToken").InnerText.Trim();
                configEntity.EnableLog = bool.Parse(node.SelectSingleNode("EnableLog").InnerText.Trim());
                configEntity.EnableFileLog = bool.Parse(node.SelectSingleNode("EnableFileLog").InnerText.Trim());
                configEntity.EnableConsoleLog = bool.Parse(node.SelectSingleNode("EnableConsoleLog").InnerText.Trim());
                configEntity.LogInfomations = bool.Parse(node.SelectSingleNode("LogInfomations").InnerText.Trim());
                configEntity.LogApiMessagees = bool.Parse(node.SelectSingleNode("LogApiMessagees").InnerText.Trim());
                configEntity.LogExceptions = bool.Parse(node.SelectSingleNode("LogExceptions").InnerText.Trim());
                configEntity.LogFileName = node.SelectSingleNode("LogFileName").InnerText.Trim();
                configEntity.SiteCode = (SiteCodeType)Enum.Parse(typeof(SiteCodeType),node.SelectSingleNode("SiteCode").InnerText.Trim());
                
                dicConfigs.Add(configName,configEntity);
            }

            return dicConfigs[configName];
        }
    }
}
